<template>
  <div>
    <home-header></home-header>
    <home-swiper :swiperList="swiperList"></home-swiper>
    <home-icons :iconList="iconList"></home-icons>
    <home-recommend :recommendList="recommendList"></home-recommend>
    <home-weekend :weekendList="weekendList"></home-weekend>
  <!-- <router-link to="/List" class="home">列表页</router-link> -->
  <!-- 类似于a标签，且to的用法和href类似 -->
  </div>
</template>

<script>
import HomeHeader from './components/Header'
import HomeSwiper from './components/Swiper'
import HomeIcons from './components/Icons'
import HomeRecommend from './components/Recommend'
import HomeWeekend from './components/Weeekend'
import axios from 'axios'
// 使用axios来进行Ajax数据请求，Vue和React都推荐使用axios
import {mapState} from 'vuex'
export default {
  name: 'Home',
  components: {
    HomeHeader,
    HomeSwiper,
    HomeIcons,
    HomeRecommend,
    HomeWeekend
  },
  data () {
    return {
      lastCity: '',
      swiperList: [],
      iconList: [],
      recommendList: [],
      weekendList: []
    }
  },
  computed: {
    ...mapState({
      city: 'city'
      // 或者city: (state) => state.city
      // 或者['city'] 或者普通函数
    })
  },
  methods: {
    getHomeInfo () {
      // axios.get('/static/mock/index.json')
      // 为了避免上线代码时更改，利用转发机制，把对api下所有json请求转发到本地的static/mock文件夹下
      // Vue里面提供proxy代理功能可以实现  在comfig/index.js文件里设置
      axios.get('/api/index.json?city=' + this.city)
        .then(this.getHomeInfoSucc)
    },
    getHomeInfoSucc (res) {
      res = res.data
      if (res.ret && res.data) {
        const data = res.data
        this.swiperList = data.swiperList
        this.iconList = data.iconList
        this.recommendList = data.recommendList
        this.weekendList = data.weekendList
      }
    }
  },
  mounted () {
    // lastCity临时缓存当前城市
    this.lastCity = this.city
    this.getHomeInfo()
  },
  activated () {
    if (this.lastCity !== this.city) {
      this.lastCity = this.city
      this.getHomeInfo()
    }
  }
}
</script>

<style>
</style>
